package com.example.second_home_work

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    val expert = ProgramExpert()
    private lateinit var linearLayout: LinearLayout
    private lateinit var buttonAddText: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        //功能区1
        linearLayout = findViewById(R.id.linearLayout)
        buttonAddText = findViewById(R.id.buttonAddText)

        // 点击添加文本
        buttonAddText.setOnClickListener {
            addNewTextView()
        }
        //功能区2
        val spinner = findViewById<Spinner>(R.id.spinner)
        val button2 = findViewById<Button>(R.id.button2)
        val textView = findViewById<TextView>(R.id.textView)

        button2.setOnClickListener {
            textView.text = expert.getlanguage(spinner.selectedItem.toString())
        }

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }
    //点击添加新文本的函数
    private fun addNewTextView() {

        val textView = TextView(this)
        textView.text = getString(R.string.active_text)
        textView.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        textView.setPadding(10, 10, 10, 10)

        linearLayout.addView(textView)
    }
}